<?php
/**
 * 角色管理
 * User: Richard
 * Date: 2020/3/15
 * Time: 13:17
 */

namespace Demo\Controller;


use Common\Controller\DemoBaseController;

class RoleController extends DemoBaseController
{
    private $roleModel;

    public function __construct()
    {
        parent::__construct();
        $this->roleModel = M('role');
    }

    /**
     * 角色列表
     */
    public function index()
    {
        $pageCount = I('count', 20, 'intval');//一页20个

        $where = ['status' => 1];
        $name = I('name');
        if (!empty($name)) {
            $where['name|desc'] = ['like', '%' . $name . '%'];
        }

        $count = $this->roleModel->where($where)->count();
        $page = $this->page($count, $pageCount);

        $list = $this->roleModel->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order('create_time desc')
            ->select();
        $this->returnok(['list' => $list, 'page' => $page]);
    }


    /**
     * 添加角色提交
     */
    public function add_post()
    {

        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写角色名称');
        }

        $desc = I('desc');

        $data = [
            'name' => $name,//角色名称
            'desc' => $desc,//角色描述
        ];
        $id = $this->roleModel->add($data);
        if ($id) {
            $this->returnok(['id' => $id], '添加成功');
        } else {
            $this->returnerror('', '添加失败，请重试');
        }
    }

    /**
     * 编辑角色
     */
    public function edit()
    {
        $roleId = I('id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '无效请求');
        }
        $roleInfo = $this->roleModel->where(['id' => $roleId, 'status' => 1])->find();
        if (empty($roleInfo)) {
            $this->returnerror('', '未找到角色或角色已被管理员删除');
        }
        $this->returnok($roleInfo);
    }

    /**
     * edit角色提交
     */
    public function edit_post()
    {
        $roleId = I('id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '无效请求');
        }
        $name = I('name');
        if (empty($name)) {
            $this->returnerror('', '请填写角色名称');
        }
        $desc = I('desc');
        $data = [
            'name' => $name,//角色名称
            'desc' => $desc,//角色描述
        ];
        $res = $this->roleModel->where(['id' => $roleId])->save($data);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $roleId], '修改成功');
        } else {
            $this->returnerror('', '修改失败，请重试');
        }
    }

    /**
     * 角色软删除
     */
    public function softDelete()
    {
        $roleId = I('id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->roleModel->where(['id' => $roleId])->save(['status' => 0]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $roleId], '删除成功');
        } else {
            $this->returnerror('', '删除失败，请重试');
        }
    }

    /**
     * 软删除橘色恢复
     */
    public function reply()
    {
        $roleId = I('id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '无效请求');
        }
        $res = $this->roleModel->where(['id' => $roleId])->save(['status' => 1]);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $roleId], '恢复成功');
        } else {
            $this->returnerror('', '恢复失败，请重试');
        }
    }

}